package main

import (
	"douhu_backend/internal/infra"
	"douhu_backend/internal/infra/logger"
	"log"
	"os"
	"os/signal"
	"syscall"
)

// @contact.name API Support
// @contact.url http://www.swagger.io/support
// @contact.email support@swagger.io
//
// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
//
// @termsOfService http://swagger.io/terms/
//
// @BasePath /api
// @securitydefinitions.iam  zta_ram_rest
func main() {
	// 初始化所有基础设施和应用
	err := infra.Init()
	if err != nil {
		log.Fatalf("Failed to initialize infrastructure: %v", err)
		return
	}

	// 设置信号处理以实现优雅关闭
	gracefulShutdown()
}

// gracefulShutdown 处理系统信号并实现优雅关闭
func gracefulShutdown() {
	// 创建信号通道
	sigChan := make(chan os.Signal, 1)

	// 注册要监听的信号
	signal.Notify(sigChan,
		syscall.SIGINT,  // Ctrl+C
		syscall.SIGTERM, // 终止信号
		syscall.SIGQUIT, // 退出信号
		syscall.SIGHUP,  // 挂起信号
	)

	logger.Info("Server started successfully. Press Ctrl+C to gracefully shutdown...")

	// 等待信号
	sig := <-sigChan
	logger.Infof("Received signal: %v, starting graceful shutdown...", sig)

	// 调用资源清理函数
	if err := infra.Close(); err != nil {
		logger.Errorf("Error during shutdown: %v", err)
	}

	logger.Info("Server shutdown completed")
}
